package com.hzcx.app.simplechat.ui.home;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.common.Common;
import com.hzcx.app.simplechat.ui.chat.GroupContactSearchActivity;
import com.hzcx.app.simplechat.ui.group.AddGroupForSearchActivity;
import com.hzcx.app.simplechat.ui.home.contract.AddFriendFirstContract;
import com.hzcx.app.simplechat.ui.home.presenter.AddFriendFirstPresenter;
import com.hzcx.app.simplechat.ui.publicui.CaptureActivity;
import com.hzcx.app.simplechat.ui.publicui.QrCodeActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.ShareMineInfoBean;
import com.hzcx.app.simplechat.util.toast.ToastUtils;
import com.hzcx.app.simplechat.util.wx.WxShareUtils;
import com.hzcx.app.simplechat.view.SegmentControlView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * 添加好友页面
 */
public class AddFriendFirstActivity extends BaseActivity<AddFriendFirstPresenter> implements AddFriendFirstContract.View {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.navigation_segment_control_view)
    SegmentControlView segmentControlView;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_zxing_title)
    TextView tvZxingTitle;
    @BindView(R.id.cons_zxing)
    ConstraintLayout consZxing;
    @BindView(R.id.tv_wechat_title)
    TextView tvWechatTitle;
    @BindView(R.id.cons_wechat)
    ConstraintLayout consWechat;
    @BindView(R.id.tv_mine_qrcode)
    TextView tvMineQrcode;
    @BindView(R.id.tv_wechat_tips)
    TextView tvWechatTips;

    private int segmentIndex = 0;

    @Override
    public int getLayout() {
        return R.layout.activity_add_friend_first;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AddFriendFirstPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("添加好友");

        tvSearch.setText("搜索手机号/" + getResources().getString(R.string.app_name) + "号");
        tvWechatTips.setText("邀请微信好友进入" + getResources().getString(R.string.app_name));

        segmentControlView.setVisibility(View.VISIBLE);
        String[] tests = {"加好友", "加群"};
        segmentControlView.setTexts(tests);
        segmentControlView.setOnSegmentChangedListener(new SegmentControlView.OnSegmentChangedListener() {
            @Override
            public void onSegmentChanged(int newSelectedIndex) {
                if (newSelectedIndex == segmentIndex) {
                    return;
                }
                segmentIndex = newSelectedIndex;
                if (segmentIndex == 0) {
                    tvSearch.setText("搜索手机号、群号");
                    tvZxingTitle.setVisibility(View.VISIBLE);
                    consZxing.setVisibility(View.VISIBLE);
                    tvWechatTitle.setVisibility(View.VISIBLE);
                    consWechat.setVisibility(View.VISIBLE);
                    tvMineQrcode.setVisibility(View.VISIBLE);
                    tvWechatTips.setVisibility(View.VISIBLE);
                } if (segmentIndex == 1) {

                    tvSearch.setText("搜索群名称/群号");
                    tvZxingTitle.setVisibility(View.GONE);
                    consZxing.setVisibility(View.GONE);
                    tvWechatTitle.setVisibility(View.GONE);
                    consWechat.setVisibility(View.GONE);
                    tvMineQrcode.setVisibility(View.GONE);
                    tvWechatTips.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.tv_search, R.id.cons_zxing, R.id.cons_wechat, R.id.tv_mine_qrcode})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_search:
                if (segmentIndex == 0) {
                    startActivity(new Intent(this, AddFriendSearchActivity.class));
                } else if (segmentIndex == 1) {
                    startActivity(new Intent(this, GroupContactSearchActivity.class));
                }
                break;

            case R.id.cons_zxing: // 扫一扫
                registerPermission();
                break;

            case R.id.cons_wechat: // 邀请好友分享
                mPresenter.getShareMineInfo(this);
                break;

            case R.id.tv_mine_qrcode: // 我的二维码图片
                startActivity(new Intent(this, QrCodeActivity.class));
                break;
        }
    }

    /**
     * 申请相机权限
     */
    private void registerPermission() {
        PermissionGen.with(this)
                .addRequestCode(Common.BASIC_PERMISSION_REQUEST_CODE)
                .permissions(Manifest.permission.CAMERA)
                .request();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    /**
     * 权限申请失败
     */
    @PermissionFail(requestCode = Common.BASIC_PERMISSION_REQUEST_CODE)
    public void openFail() {
        ToastUtils.show("权限获取失败，无法保存图片");
    }

    /**
     * 权限申请成功
     */
    @PermissionSuccess(requestCode = Common.BASIC_PERMISSION_REQUEST_CODE)
    public void openSuccess() {
        startActivity(new Intent(AddFriendFirstActivity.this, CaptureActivity.class));
    }

    /**
     * 分享内容获取成功
     *
     * @param data
     */
    @Override
    public void shareResult(ShareMineInfoBean data) {
        Glide.with(this).asBitmap().load(data.getAvatar()).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                hideLoading();
                WxShareUtils.shareWeb(AddFriendFirstActivity.this, Common.WX_APP_ID, data.getShare_url(),
                        data.getTitle(), data.getDescription(), resource, 0);
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
                hideLoading();
                showError("头像下载失败");
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
